//
// Created by 先通明 on 24/08/2018.
//todo implement stack

#include "stack.h"

void initalizeStack(Stack *ps) {
    ps->top = NULL;
    ps->size = 0;
}

bool stackIsFull(const Stack *ps) {
    if (ps->top == NULL)
        return false;
    if (ps->size >= MAXSTACK)
        return true;
    return false;
}

bool stackIsEmpty(const Stack *ps) {
    if (ps->top == NULL) {
        return true;
    }
    return false;
}

int stackItemCount(const Stack *ps) {
    return ps->size;
}

bool pushStack(Item item, Stack *ps) {
    if (stackIsFull(ps)) {
        return false;
    }
    Node *node = malloc(sizeof(Node));
    node->item = item;
    node->prevous = ps->top;
    ps->top = node;
    return true;
}

bool popStack(Item* pi,Stack * ps){
    if (stackIsEmpty(ps)) {
        return false;
    }
    *pi = ps->top->item;
    ps->top = ps->top->prevous;
    return true;
}

void emptyStack(Stack * ps){
    while (ps->top!=NULL){
        Node* temp = ps->top->prevous;
        free(ps->top);
        ps->top = temp;
    }
}